package java_class_work.week08.internet.demo1;

import java.io.IOException;
import java.net.*;

/**
 * @Author Tiam
 * @Date 2022/10/31 17:22
 * @Description:
 */
public class UdpSend1 {
    public static void main(String[] args) {
        DatagramSocket datagramSocket = null;
        try {
            // 1. 建立Socket UDP服务
            datagramSocket = new DatagramSocket(6666);

            // 2. 准备需要发送的数据
            byte[] bytes = "这是一个测试数据".getBytes();
            DatagramPacket datagramPacket = new DatagramPacket(bytes, bytes.length, InetAddress.getByName("127.0.0.1"), 8888);

            // 3. 发送数据
            datagramSocket.send(datagramPacket);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 4. 关闭连接
            if (datagramSocket != null) {
                datagramSocket.close();
            }
        }
    }
}
